package com.apes.pi.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 管道
 * 定义 filter、interceptor、transformer
 *
 * @author wuq
 * @create 2019-10-19 10:19
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Pipe.CONST_TABLE_NAME)
public class Pipe extends Model {
    public static final String CONST_TABLE_NAME = "pi_Pipe";

    @Id
    @Column(name = "id", nullable = false, length = 8)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "PI管道")
    @Comment("管道编码")
    private String id;

    @Column(name = "parent_id", nullable = false, length = 120)
    @Comment("父级编码(接收者、队列、队列接收者)")
    private String parent;  // 接收者、队列、队列接收者

    @Column(name = "parent_type", length = 120)
    @Comment("父级类型(receiver、queue、queueReceiver)")
    private String parentType;

    @Column(name = "name", length = 200)
    @Comment("管道描述")
    private String name; // 描述

    @Column(name = "position", nullable = false, length = 10)
    @Comment("管道场景")
    private String position;   // 前置，后置（before，after）

    @ManyToOne
    @JoinColumn(name = "pattern", referencedColumnName = "id")
    @Comment("模式")
    private Enumeration pattern; // 模式（filter、interceptor、transformer）

    @OneToMany(mappedBy = "pipe", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    private List<PipeParams> params;

    public List<PipeParams> getParams() {
        if (params == null || params.isEmpty()) return new ArrayList<>();
        for (PipeParams item : params) {
            item.setPipe(this);
        }
        return params;
    }

    @Transient
    private List<PipeParams> beforePipesParams;

    @Transient
    private List<PipeParams> afterPipesParams;

    public List<PipeParams> getBeforePipesParams() {
        return this.params;
    }

    public List<PipeParams> getAfterPipesParams() {
        return this.params;
    }
}
